%%%---------------------------------------------------------------------------------------
%%% @author    Stuart Jackson <sjackson@simpleenigma.com> [http://erlsoft.org]
%%% @copyright 2006 - 2008 Simple Enigma, Inc. All Rights Reserved.
%%% @doc       Elestic Cloud Computing Libraray for version 2008-05-05
%%% @reference See <a href="http://erlsoft.org/modules/eifaws" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://eifaws.googlecode.com" target="_top">ErlMail Google Code Repository</a> for more information
%%% @version   0.0.1
%%% @since     0.0.1
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2008 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------

-module(eifaws_ec2_2008_05_05).
-author('sjackson@simpleenigma.com').
-include("../include/eifaws.hrl").
-include_lib("xmerl/include/xmerl.hrl").
-export([ec2/2]).

ec2('AttachVolume',Message) ->
	XMLRoot = eifaws_util:to_xml(Message),
	[VolumeId,InstanceId,Device,Status,AttachTime] = 
		eifaws_util:xml_extract(XMLRoot,
			[{string,"volumeId"},{string,"instanceId"},{string,"device"},{string,"status"},{string,"attachTime"}]),
	#volume_info{volume_id = VolumeId, instance_id = InstanceId, device = Device, volume_status = Status,attach_time = AttachTime};

ec2('CreateSnapshot',Message) ->
	XMLRoot = eifaws_util:to_xml(Message),
	[SnapshotId,Status,VolumeId,StartTime,Progress] = 
		eifaws_util:xml_extract(XMLRoot,
			[{string,"snapshotId"},{string,"status"},{string,"volumeId"},{string,"startTime"},{string,"progress"}]),
	#snapshot_info{snapshot_id = SnapshotId, volume_id = VolumeId, status = Status, start_time = StartTime, progress = Progress};

ec2('CreateVolume',Message) ->
	XMLRoot = eifaws_util:to_xml(Message),
	[VolumeId,Size,Status,CreateTime,Zone,SnapshotId] = 
		eifaws_util:xml_extract(XMLRoot,
			[{string,"volumeId"},{integer,"size"},{string,"status"},{string,"createTime"},{string,"zone"},{string,"snapshotId"}]),
	#volume_info{volume_id = VolumeId, size = Size, volume_status = Status, create_time = CreateTime, availability_zone = Zone, snapshot_id = SnapshotId};

ec2('DeleteSnapshot',Message) -> 
	XMLRoot = eifaws_util:to_xml(Message),
	eifaws_util:aws_atom(XMLRoot);

ec2('DeleteVolume',Message) -> 
	XMLRoot = eifaws_util:to_xml(Message),
	eifaws_util:aws_atom(XMLRoot);

ec2('DetachVolume',Message) ->
	XMLRoot = eifaws_util:to_xml(Message),
	[VolumeId,InstanceId,Device,Status,AttachTime] = 
		eifaws_util:xml_extract(XMLRoot,
			[{string,"volumeId"},{string,"instanceId"},{string,"device"},{string,"status"},{string,"attachTime"}]),
	#volume_info{volume_id = VolumeId, instance_id = InstanceId, device = Device, volume_status = Status,attach_time = AttachTime};
ec2('DescribeSnapshots',_Message) -> eifaws_util:not_implimented();



ec2('DescribeVolumes',Message) ->
	XMLRoot = eifaws_util:to_xml(Message),
	Items = eifaws_util:xml_list(XMLRoot,"volumeSet/item"),
	lists:foldr(fun(E,Acc) -> 
		[VolumeId,Size,VolumeStatus,CreateTime] = 
			eifaws_util:xml_extract(E,[{string,"volumeId"},{integer,"size"},{string,"status"},{string,"createTime"}]),
		AttachList = eifaws_util:xml_list(E,"attachmentSet/item"),
		Attachments = lists:foldr(fun(A,Acc2) -> 
			[VolumeId,InstanceId,Size,Status,AttachTime] = eifaws_util:xml_extract(A,[{string,"volumeId"},{string,"instanceId"},{integer,"size"},{string,"status"},{string,"attachTime"}]),
			Attach = #attach_info{volume_id = VolumeId,instance_id = InstanceId, size = Size, status = Status, attach_time = AttachTime},
			[Attach|Acc2]
		end,[],AttachList),
		VolumeInfo = #volume_info{volume_id = VolumeId, size = Size,volume_status = VolumeStatus, create_time = CreateTime,attachments = Attachments},
		[VolumeInfo|Acc]
	end, [], Items);




%%%% Check Previous Version

ec2(Action,Message) -> eifaws_ec2_2008_02_01:ec2(Action,Message).








